package org.fengling.readingplatform.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.fengling.readingplatform.manager.service.AuthorService;
import org.fengling.readingplatform.model.dto.author.AuthorDto;
import org.fengling.readingplatform.model.entity.author.Author;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "作家接口")
@RestController
@RequestMapping(value="/admin/author")
public class AuthorController {

    @Autowired
    private AuthorService authorService;

    //1. 作家分页查询
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             AuthorDto authorDto){
        PageInfo<Author> pageInfo = authorService.findByPage(pageNum, pageSize, authorDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    //2. 作家添加方式
    @PostMapping(value = "/saveAuthor")
    public Result saveAuthor(@RequestBody Author author){
        authorService.saveAuthor(author);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //3. 作家删除的方法
    @DeleteMapping("/deleteById/{authorId}")
    public Result deleteById(@PathVariable("authorId") Long authorId){
        authorService.deleteById(authorId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //4. 作家修改的方法
    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody Author author){
        authorService.updateAuthor(author);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
